Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parent-package-json

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parent-package-json

Find, read and parse the package.json of the parent module

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

parent-package-json

Build Status Build status Coverage Status dependencies Status devDependencies Status XO code style

Using parent-package-json, you can find the parent package.json, so the package.json of the module that uses your module.

npm install --save parent-package-json
var parent = require('parent-package-json');

Getting started

For getting the path to the parent package.json of the executing module (so the module that runs this code), simply do:

var pathToParent = parent().path;

If you're not sure if there's always a parent package.json, you can check first, too:

var pathToParent = parent(); // Will return false if no parent exists

if(pathToParent !== false) {
  pathToParent = pathToParent.path;
}

Usually, parent-package-json will use process.cwd() for starting at, it will search the parent folders up until / for finding a package.json (and stops as soon as it finds one). If you want it to start somewhere other than process.cwd(), provide a path as an argument instead:

var pathToParentOfCustomPath = parent('/My/Cool/Folder').path;

If you want to ignore a package.json (for example to find the parent package.json of the parent module), you can pass an ignore parameter (default: 0) saying how many package.jsons you want to ignore when searching:

var pathToParentOfParent = parent(null, 1).path; // Or, even more complicated:
var pathToParentOfParentOfCustomPath = parent('/My/Cool/Folder', 1).path;

Note: The module's own package.json is always ignored, even if the ignore parameter equals 0

Processing the data

parent-package-json also allows you reading the content of a package.json, and even parsing its JSON right away.

For reading its content, do:

var contentOfParent = parent().read();

If you want to parse its JSON code, you can run:

var JSONOfParent = parent().parse();
var versionOfParent = JSONOfParent.version;

// Or

var versionOfParent = parent().parse().version;

Keywords

FAQs

Package last updated on 30 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc